﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using tenpay;
using System.Collections;

namespace NJL.Web.Payment.tenpay
{
    public partial class index :BasePageVisitor
    {
        private readonly NJL.BLL.NJL_BOOK_ORDER bllOrder = new NJL.BLL.NJL_BOOK_ORDER();
        protected NJL.Model.NJL_BOOK_ORDER modelOrder;
        protected NJL.BLL.NJL_SYS_CS_PAY_MAP bllPayMap = new NJL.BLL.NJL_SYS_CS_PAY_MAP();
        NJL.Model.NJL_SYS_CS_PAY_MAP modelPayMap;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelPayMap = bllPayMap.GetModel("2");

            //商户号
            string bargainor_id = modelPayMap.PAY_COMPANY_ID;

            //密钥
            string key = modelPayMap.PAY_COMPANY_KEY;

            //当前时间 yyyyMMdd
            string date = DateTime.Now.ToString("yyyyMMdd");

            //生成订单10位序列号，此处用时间和随机数生成，商户根据自己调整，保证唯一
            //string strReq = "" + DateTime.Now.ToString("HHmmss") + TenpayUtil.BuildRandomStr(4);
            string strReq = Request.Form["hidOrderId"];
            //商户订单号，不超过32位，财付通只做记录，不保证唯一性
            string sp_billno = strReq;

            //财付通订单号，10位商户号+8位日期+10位序列号，需保证全局唯一
            string transaction_id = bargainor_id + date + strReq.Remove(0, 1);
            string return_url = "http://" + WEBSITE_DOMAIN + "/Payment/tenpay/return_url.aspx";

            //创建PayRequestHandler实例
            PayRequestHandler reqHandler = new PayRequestHandler(Context);
            string transaction_price = string.Format("{0:0}", (Convert.ToDecimal(Request.Form["txtAmount"]) * 100));
            //设置密钥
            reqHandler.setKey(key);

            //初始化
            reqHandler.init();

            //-----------------------------
            //设置支付参数
            //-----------------------------
            reqHandler.setParameter("bargainor_id", bargainor_id);			//商户号
            reqHandler.setParameter("sp_billno", sp_billno);				//商家订单号
            reqHandler.setParameter("transaction_id", transaction_id);		//财付通交易单号
            reqHandler.setParameter("return_url", return_url);				//支付通知url
            reqHandler.setParameter("desc", "订单号：" + transaction_id);	//商品名称
            reqHandler.setParameter("total_fee", transaction_price);	    //商品金额,以分为单位
            reqHandler.setParameter("cs", "utf-8");                         //设置编码




            //用户ip,测试环境时不要加这个ip参数，正式环境再加此参数
            reqHandler.setParameter("spbill_create_ip", Page.Request.UserHostAddress);

            //获取请求带参数的url
            //string requestUrl = reqHandler.getRequestURL();

            //string a_link = "<a target=\"_blank\" href=\"" + requestUrl + "\">" + "财付通支付" + "</a>";

            //Response.Write(a_link);


            //post实现方式

            reqHandler.getRequestURL();
            Response.Write("<form  method=\"post\" action=\"" + reqHandler.getGateUrl() + "\"> \n");
            Hashtable ht = reqHandler.getAllParameters();
            foreach (DictionaryEntry de in ht)
            {
                Response.Write("<input type=\"hidden\" name=\"" + de.Key + "\" value=\"" + de.Value + "\" >\n");
            }
            Response.Write("</form>\n<script type=\"text/javascript\">document.forms[0].submit();</script>");


            //获取debug信息
            //string debuginfo = reqHandler.getDebugInfo();
            //Response.Write("<br/>" + debuginfo + "<br/>");

            //重定向到财付通支付
            //reqHandler.doSend();                      
        }
    }
}